package com.jinghong.epidemiccontrolproject.service.impi;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jinghong.epidemiccontrolproject.mapper.PatientMapper;
import com.jinghong.epidemiccontrolproject.mapper.RegionMapper;
import com.jinghong.epidemiccontrolproject.pojo.PageBean;
import com.jinghong.epidemiccontrolproject.pojo.Patient;
import com.jinghong.epidemiccontrolproject.pojo.Region;
import com.jinghong.epidemiccontrolproject.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * ClassName:PatientServiceImpi
 * Package:com.jinghong.epidemiccontrolproject.service.impi
 * Description:
 *
 * @Author: 寻月君子
 * @CreatTime:2024/1/19 23:06
 * Version:1.0
 */
@Service
public class PatientServiceImpi implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, Integer age1, Integer age2, Integer regionId, LocalDate begin, LocalDate end, Short status) {
        //1. 设置分页参数
        PageHelper.startPage(page, pageSize);

        //2. 执行查询
        List<Patient> patientList = patientMapper.list(name, gender, age1, age2, regionId, begin, end, status);
        Page<Patient> p = (Page<Patient>) patientList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        //删除病人
        patientMapper.delete(ids);
        //拿到病人信息
        List<Patient> patients = patientMapper.get(ids);
        //
        for (Patient patient : patients) {
            //重新统计赋值病人所属地区该病情人数
            countAndUpdate();
        }

    }

    @Override
    public void save(Patient patient) {
        patient.setUpdateTime(LocalDate.now());
        //添加病人
        patientMapper.insert(patient);
        //重新统计赋值病人所属地区该病情人数
        countAndUpdate();
    }

    @Override
    public Patient getById(Integer id) {
        return patientMapper.getById(id);
    }


    @Override
    public void update(Patient patient) {
        //更改信息
        patient.setUpdateTime(LocalDate.now());
        patientMapper.update(patient);
        //重新统计赋值病人所属地区该病情人数
        countAndUpdate();
    }

    @Override
    public void deleteById(Integer id) {
        //删除病人
        patientMapper.deleteById(id);
        //拿到病人信息
        Patient patient = patientMapper.getById(id);
        //
        //重新统计赋值病人所属地区该病情人数
        countAndUpdate();
    }


    //重新统计赋值病人所属地区该病情人数
    public void countAndUpdate() {
        //重新统计地区病情人数
        List<Region> regions =  regionMapper.list("");
        for (Region region : regions) {
            for (Short status = 1;status <=3 ; status++) {
                Integer updateNum = patientMapper.Count(region.getId(),status);
                regionMapper.update(region.getId(), status, updateNum);
            }

        }
        //重新赋值

    }
}
